package com.fortune.controller;

import java.util.List;

import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.StoreMgtExcution;
import com.fortune.dto.in.storeMgt.StoreFindDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.StoreMgtEnum;
import com.fortune.service.StoreMgtService;
import com.fortune.util.UUIDUtil;
import io.swagger.annotations.ApiImplicitParams;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.in.storeMgt.StoreAddDto;
import com.fortune.dto.in.storeMgt.StoreEditDto;
import com.fortune.vo.storeMgt.StoreInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "storeMgt", description = "仓库管理", tags = {"仓库管理"})
@RestController
@RequestMapping("storeMgt")
public class StoreMgtController {

	@Autowired
	private StoreMgtService storeMgtService;

	@ApiOperation(value = "仓库列表", nickname = "storeMgtList", notes = "", tags={ "@安童" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="search", value="搜索",paramType="query",dataType="String"),
			@ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
			@ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData> list(HttpServletRequest request,@ApiIgnore @Validated StoreFindDto storeFindDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		storeFindDto.setPage((storeFindDto.getPage()-1)*10);
		List<StoreInfo> list = storeMgtService.findStoreList(storeFindDto,mid);
		int count = storeMgtService.findStoreListCount(storeFindDto,mid);
		PagingData pagingData = new PagingData();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new StoreMgtExcution<PagingData>(pagingData,StoreMgtEnum.FIND_SUCCESS);
	}
	
	@ApiOperation(value = "添加仓库", nickname = "storeMgtAdd", notes = "", tags={ "@安童" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody StoreAddDto addDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		String id = UUIDUtil.get32UUID();
		int add = storeMgtService.insertStore(addDto,mid,id);
		if(add > 0){
			return new StoreMgtExcution<Void>(null,StoreMgtEnum.ADD_SUCCESS);
		}else{
			if(add == -1){
				return new StoreMgtExcution<Void>(null,StoreMgtEnum.FIND_REPEAT_FALSE);
			}
			return new StoreMgtExcution<Void>(null,StoreMgtEnum.ADD_FALSE);
		}
	}
	
	@ApiOperation(value = "添加仓库", nickname = "storeMgtEdit", notes = "", tags={ "@安童" })
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody StoreEditDto editDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		int add = storeMgtService.editStore(editDto,mid);
		if(add > 0){
			return new StoreMgtExcution<Void>(null,StoreMgtEnum.EDIT_SUCCESS);
		}else{
			if(add == -1){
				return new StoreMgtExcution<Void>(null,StoreMgtEnum.FIND_REPEAT_FALSE);
			}
			return new StoreMgtExcution<Void>(null,StoreMgtEnum.EDIT_FALSE);
		}
	}
	
	@ApiOperation(value = "删除仓库", nickname = "storeMgtDelete", notes = "", tags={ "@安童" })
	@ApiImplicitParam(name="storeId", value="仓库id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="仓库id不能为空")String storeId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		int delete = storeMgtService.deleteStore(mid,storeId);
		if(delete > 0){
			return new StoreMgtExcution<Void>(null,StoreMgtEnum.DELETE_SUCCESS);
		}else{
			return new StoreMgtExcution<Void>(null,StoreMgtEnum.DELETE_FALSE);
		}
	}
}
